<?php
namespace app\examScore\controller;
use app\common\Base;
use app\common\TeacherModel;
use app\common\Excel;
use think\Db;
//--科任教师
class SubjectTeacher extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		if(request()->isPost()){
			$page = input('page');
			$rows = input('rows');
			$where["a.年级号"] = array("gt", currTerm()["finishedKey"]);
			$grade = input("grade");
			if($grade) {
				$array = explode("_", $grade);
				$where["a.年级号"] = array("eq", $array[0]);
				$where["a.学籍类型"] = array("eq", $array[1]);
			}
			$Model = new TeacherModel();
			return $Model->getClassTeacherList($page, $rows, $where);
		}
	}

	public function addUpdate() {
		$all = request()->param();
		try {
			update('班主任科任', $all);
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function del() {
		$delIds = input("delIds");
		try {
			Db::table("班主任科任")->delete(explode(",", $delIds));
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function exportExcel() {
		$where["a.年级号"] = array("gt", currTerm()["finishedKey"]);
		$grade = input("grade");
		if($grade) {
			$array = explode("_", $grade);
			$where["a.年级号"] = array("eq", $array[0]);
			$where["a.学籍类型"] = array("eq", $array[1]);
		}
		$Model = new TeacherModel();
		$result = $Model->getClassTeacherList(1, PHP_INT_MAX, $where);
		//excel表头内容
		$header = array('年级号'=>'gradeName','班号'=>'班号','班主任'=>'班主任','语文'=>'语文','数学'=>'数学','英语'=>'英语','政治'=>'政治','历史'=>'历史','体育'=>'体育','物理'=>'物理','化学'=>'化学','地理'=>'地理','生物'=>'生物');
		Excel::toExcel($result["rows"],'科任教师',$header);
		exit;
	}

	function importExcel() {
	    //上传excel文件
	    $file = request()->file('files');
	    if(!$file) {
	    	return "请选择文件";
	    }
	    $data = Excel::importExcel($file);
	    if(count($data) == 0) {
	    	return "没有数据！";
	    }
	    try {
	    	$errorList = [];
    		$resultList = [];
    		$teacherList = Db::table("教师信息")->field("ID value, 姓名 text")->select();
		    foreach($teacherList as $key => $obj){
		        $teacherMap[$obj["text"]] = $obj["value"];
		    }
    		foreach ($data as $idx => $obj) {
    			$result = $obj;
    			if (!empty($obj["班主任"])) {
    				if(array_key_exists($obj["班主任"], $teacherMap)) {
    					$result["班主任ID"] = $teacherMap[$obj["班主任"]];
    				}else {
    					$errorList[count($errorList)] = "第".$idx."行的班主任不存在";
    				}
    			}else {
    				$errorList[count($errorList)] = "第".$idx."行的班主任不能为空";
    			}
    			if(!empty($obj["年级号"])) {
    				$gl = gradeMap();
    				foreach ($gl as $key => $value) {
						if($value == $obj["年级号"]) {
							$array = explode("_", $key);
							$result["年级号"] = $array[0];
    						$result["学籍类型"] = $array[1];
							break;
						}
					}
    			}
    			unset($result["班主任"]);
    			$resultList[$idx] = $result;
    		}
    		if(count($errorList) > 0) {
    			return implode(",", $errorList);
    		}
			Db::table('班主任科任')->insertAll($resultList);
	    } catch (\Exception $e) {
	    	return $e->getMessage();
	    }
	    return "000";
	}

}
